package cn.edu.cug.cs.gtl.apps.ui;

import cn.edu.cug.cs.gtl.utils.impl.UserManagerImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.ResourceBundle;

public class LoginDialog extends JDialog {
    static ResourceBundle resourceBundle=ResourceBundle.getBundle("app_resource", Locale.getDefault());

    UserManagerImpl userManager = new UserManagerImpl();

    private JButton registerButton;
    private JButton loginButton;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private boolean loginSuccess = false; // 用于跟踪登录是否成功

    public LoginDialog(JFrame owner) {
        super(owner, resourceBundle.getString("login.title"), true);
        initComponents();
        pack();
        setLocationRelativeTo(owner);
    }

    private void initComponents() {
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        setSize(300, 200);

        usernameField = new JTextField(20);
        passwordField = new JPasswordField(20);

        // 创建按钮面板，并设置布局管理器为 FlowLayout
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));

        // 添加注册按钮，并为其添加事件监听器
        registerButton = new JButton(resourceBundle.getString("login.register"));
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onRegister(); // 这里调用注册事件的处理方法
            }
        });
        buttonPane.add(registerButton);

        // 添加登录按钮，并为其添加事件监听器
        loginButton = new JButton(resourceBundle.getString("login.login"));
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onLogin();
            }
        });
        buttonPane.add(loginButton);

        // 调整组件间距，使用BoxLayout.Y_AXIS的默认间距
        add(Box.createVerticalStrut(20)); // 上部间距
        add(new JLabel(resourceBundle.getString("login.username")));
        add(Box.createVerticalStrut(10)); // 用户名和文本字段之间的间距
        add(usernameField);
        add(Box.createVerticalStrut(20)); // 用户名和密码之间的间距
        add(new JLabel(resourceBundle.getString("login.password")));
        add(Box.createVerticalStrut(10)); // 密码和文本字段之间的间距
        add(passwordField);
        add(Box.createVerticalStrut(20)); // 密码和按钮之间的间距
        add(buttonPane);
        add(Box.createVerticalStrut(20)); // 按钮和底部之间的间距
    }

    private void onRegister() {
        // 实例化注册窗口并显示
        dispose(); // 关闭登录对话框
        RegisterDialog registerDialog = new RegisterDialog(null); // 传入当前应用程序的Frame
        registerDialog.setVisible(true);

    }


    private void onLogin() {
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());

        // 检查用户名和密码是否为空
        if(username.isEmpty() || password.isEmpty()){
            JOptionPane.showMessageDialog(this, resourceBundle.getString("login.empty"), "", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 验证用户名和密码（这里使用示例值，您需要替换为实际的验证逻辑）
        if (userManager.login(username, password) != null) {
            loginSuccess = true; // 登录成功
            dispose(); // 关闭登录对话框
        } else {
            loginSuccess = false; // 登录失败
            JOptionPane.showMessageDialog(this, resourceBundle.getString("login.failed"), "", JOptionPane.ERROR_MESSAGE);
        }
    }

    public boolean isLoginSuccess() {
        return loginSuccess;
    }
}
